package org.jeecg.modules.room.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.room.entity.RoomInfo;
import org.jeecg.modules.room.entity.RoomRepairPlan;
import org.jeecg.modules.room.service.IRoomInfoService;
import org.jeecg.modules.room.service.IRoomRepairPlanService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.room.vo.RoomRepairPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 维修计划表
 * @Author: jeecg-boot
 * @Date:   2024-01-02
 * @Version: V1.0
 */
@Api(tags="维修计划表")
@RestController
@RequestMapping("/room/roomRepairPlan")
@Slf4j
public class RoomRepairPlanController extends JeecgController<RoomRepairPlan, IRoomRepairPlanService> {
	@Autowired
	private IRoomRepairPlanService roomRepairPlanService;
	@Autowired
	private IRoomInfoService roomInfoService;

	/**
	 * 分页列表查询
	 *
	 * @param roomRepairPlan
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "维修计划表-分页列表查询")
	@ApiOperation(value="维修计划表-分页列表查询", notes="维修计划表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<RoomRepairPlan>> queryPageList(RoomRepairPlan roomRepairPlan,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<RoomRepairPlan> queryWrapper = QueryGenerator.initQueryWrapper(roomRepairPlan, req.getParameterMap());
		Page<RoomRepairPlan> page = new Page<RoomRepairPlan>(pageNo, pageSize);
		IPage<RoomRepairPlan> pageList = roomRepairPlanService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	@GetMapping(value = "/queryUnableDate")
	public Result<List<RoomRepairPlanVo>> queryUnableDate(@RequestParam(name="roomCode",required=true) String roomCode) {
		 return Result.OK(roomRepairPlanService.selectUnableDate(roomCode));
	 }




	 /**
	 *   添加
	 *
	 * @param roomRepairPlan
	 * @return
	 */
	@AutoLog(value = "维修计划表-添加")
	@ApiOperation(value="维修计划表-添加", notes="维修计划表-添加")
	//@RequiresPermissions("org.jeecg.modules:room_repair_plan:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RoomRepairPlan roomRepairPlan) {

		Page<RoomInfo> page = new Page<RoomInfo>(0, 10);
		IPage<RoomInfo> roomInfoList = roomInfoService.selectUsableRoomList(page,"",roomRepairPlan.getRoomCode(),"","","",roomRepairPlan.getBeginTime(),roomRepairPlan.getEndTime());
		if(roomInfoList == null || roomInfoList.getRecords().size() == 0)
		{
			return Result.error("当前房间已经占用，不能在该时间段进行维修，请核实！");
		}

		roomRepairPlan.setRepairStatus("00"); //计划中
		roomRepairPlanService.save(roomRepairPlan);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param roomRepairPlan
	 * @return
	 */
	@AutoLog(value = "维修计划表-编辑")
	@ApiOperation(value="维修计划表-编辑", notes="维修计划表-编辑")
	//@RequiresPermissions("org.jeecg.modules:room_repair_plan:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RoomRepairPlan roomRepairPlan) {
		roomRepairPlanService.updateRepairPlan(roomRepairPlan);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "维修计划表-通过id删除")
	@ApiOperation(value="维修计划表-通过id删除", notes="维修计划表-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:room_repair_plan:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		RoomRepairPlan roomRepairPlan = roomRepairPlanService.getById(id);
		if(roomRepairPlan.getRepairStatus().equals("20")){
			return Result.error("已完成不可删除!");
		}
		roomRepairPlanService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "维修计划表-批量删除")
	@ApiOperation(value="维修计划表-批量删除", notes="维修计划表-批量删除")
	//@RequiresPermissions("org.jeecg.modules:room_repair_plan:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {

		List<String> idList = Arrays.asList(ids.split(","));
		for(String id :idList) {
			RoomRepairPlan roomRepairPlan = roomRepairPlanService.getById(id);
			if (roomRepairPlan.getRepairStatus().equals("20")) {
				return Result.error("已完成不可删除!");
			}
		}
		this.roomRepairPlanService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "维修计划表-通过id查询")
	@ApiOperation(value="维修计划表-通过id查询", notes="维修计划表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<RoomRepairPlan> queryById(@RequestParam(name="id",required=true) String id) {
		RoomRepairPlan roomRepairPlan = roomRepairPlanService.getById(id);
		if(roomRepairPlan==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(roomRepairPlan);
	}



}
